<?php

class Users_model extends CI_Model
{
  private $module = 'users';
  private $table = 'users';

  function getsearchContent($limit, $page)
  {
    $this->db->select(PREFIX . $this->table . '.*', PREFIX . 'groups.name as group_name');
    $this->db->join(PREFIX . 'groups', PREFIX . 'groups.id=' . PREFIX . $this->table . '.group_id', 'LEFT');
    if ($this->session->userdata('userInfo') != 'root') {
      $this->db->where('username != "root"');
    }
    $this->db->limit($limit, $page);
    $this->db->order_by($this->input->post('func_order_by'), $this->input->post('order_by'));
    if ($this->input->post('content') != '' && $this->input->post('content') != 'type here...') {
      $this->db->where('(`username` LIKE "%' . $this->input->post('content') . '%" )');
    }
    if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') == '') {
      $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
    }
    if ($this->input->post('dateFrom') == '' && $this->input->post('dateTo') != '') {
      $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
    }
    if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') != '') {
      $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
      $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
    }
    $query = $this->db->get(PREFIX . $this->table);

    if ($query->result()) {
      return $query->result();
    } else {
      return false;
    }
  }

  function getTotalsearchContent()
  {
    $this->db->select('*');
    $this->db->join(PREFIX . 'groups', PREFIX . 'groups.id=' . PREFIX . $this->table . '.group_id', 'LEFT');
    if ($this->session->userdata('userInfo') != 'root') {
      $this->db->where('username != "root"');
    }
    if ($this->input->post('content') != '' && $this->input->post('content') != 'type here...') {
      $this->db->where('(`username` LIKE "%' . $this->input->post('content') . '%")');
    }
    if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') == '') {
      $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
    }
    if ($this->input->post('dateFrom') == '' && $this->input->post('dateTo') != '') {
      $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
    }
    if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') != '') {
      $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
      $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
    }
    $query = $this->db->count_all_results(PREFIX . $this->table);

    if ($query > 0) {
      return $query;
    } else {
      return false;
    }
  }

  function checkLogin($user)
  {
    $this->db->select('*');
    $this->db->where('username', $user);
    $this->db->where('status', 1);
    $query = $this->db->get(PREFIX . 'users');

    foreach ($query->result() as $row) {
      $pass = $row->password;
    }

    if (!empty($pass)) {
      return $pass;
    } else {
      return false;
    }
  }
}